package com.example.hps.greendaotest.dao;

import android.content.Context;

import com.example.hps.greendaotest.entity.User;
import com.example.hps.greendaotest.gen.DaoSession;
import com.example.hps.greendaotest.gen.UserDao;

/**
 * Created by hps on 17-5-1.
 */

public class UserHelper {
    private UserDao userDao;
    private static Context mContext;
    private static UserHelper instance;

    private UserHelper() {}

    public static UserHelper getInstance(Context context) {
        if(instance == null) {
            instance = new UserHelper();
            if (mContext == null)
                mContext = context;
            DaoSession session = DbHelper.getDaoSession(mContext);
            instance.userDao = session.getUserDao();
        }
        return instance;
    }

    public long insert(User user) {
        User user1 = selectByKey(user.getId());
        if(user1 == null) {
            return userDao.insert(user);
        }
        return -1;
    }

    public User selectByKey(long id) {
        return userDao.queryBuilder().where(UserDao.Properties.Id.eq(id)).unique();
    }
}
